<?php
session_start();
if (!isset($_SESSION['user_id'])) {
    header('Location: login21.php');
    exit;
}

require_once 'teacher21.php';

$error = '';
$success = '';

$teacherModel = new Teacher();
$departments = $teacherModel->getDepartments();

if ($_SERVER['REQUEST_METHOD'] == 'POST') {
    $name = $_POST['teacher_name'] ?? '';
    $gender = $_POST['gender'] ?? '';
    $title = $_POST['title'] ?? '';
    $contact_info = $_POST['contact_info'] ?? '';
    $department_id = $_POST['department_id'] ?? '';
    $age = $_POST['age'] ?? '';
    $basic_salary = $_POST['basic_salary'] ?? '';
    $bonus = $_POST['bonus'] ?? '';
    
    if (empty($name)) {
        $error = '教师姓名不能为空';
    } elseif (empty($gender)) {
        $error = '请选择性别';
    } elseif (empty($department_id)) {
        $error = '请选择所属系部';
    } elseif (!is_numeric($age) || $age <= 0) {
        $error = '年龄必须是正数';
    } elseif (!is_numeric($basic_salary) || $basic_salary < 0) {
        $error = '基本工资必须是非负数';
    } elseif (!is_numeric($bonus) || $bonus < 0) {
        $error = '奖金必须是非负数';
    } else {
        try {
            $teacherModel->createTeacher($name, $gender, $title, $contact_info, $department_id, $age, $basic_salary, $bonus);
            header("Location: teachers21.php?success=教师添加成功");
            exit;
        } catch (Exception $e) {
            $error = $e->getMessage();
        }
    }
}
?>

<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>教学服务系统 - 添加教师</title>
    <link rel="stylesheet" href="styles.css">
</head>
<body>
    <div class="header">
        <div class="container">
            <h1>教学服务系统</h1>
        </div>
    </div>

    <div class="main-nav">
        <div class="container">
            <ul>
                <li><a href="index.php">主页</a></li>
                <li><a href="departments21.php">系部管理</a></li>
                <li><a href="classes21.php">班级管理</a></li>
                <li><a href="students21.php">学生管理</a></li>
                <li><a href="teachers21.php">教师管理</a></li>
                <li><a href="courses21.php">课程管理</a></li>
                <li><a href="scores21.php">成绩管理</a></li>
                <li><a href="teachings21.php">授课管理</a></li>
                <li class="logout"><a href="logout21.php">退出登录</a></li>
            </ul>
        </div>
    </div>

    <div class="container">
        <div class="card">
            <div class="card-header">
                <h2 class="card-title">添加教师</h2>
            </div>
            
            <?php if ($error): ?>
            <div class="message message-error"><?php echo $error; ?></div>
            <?php endif; ?>
            
            <div class="card-body">
                <form method="post">
                    <div class="form-group">
                        <label for="teacher_name">教师姓名</label>
                        <input type="text" id="teacher_name" name="teacher_name" required
                               class="form-control" placeholder="请输入教师姓名" maxlength="50">
                    </div>
                    
                    <div class="form-group">
                        <label>性别</label>
                        <div class="radio-group">
                            <label>
                                <input type="radio" name="gender" value="男" required> 男
                            </label>
                            <label>
                                <input type="radio" name="gender" value="女" required> 女
                            </label>
                        </div>
                    </div>
                    
                    <div class="form-group">
                        <label for="title">职称</label>
                        <input type="text" id="title" name="title"
                               class="form-control" placeholder="请输入职称" maxlength="50">
                    </div>
                    
                    <div class="form-group">
                        <label for="contact_info">联系方式</label>
                        <input type="text" id="contact_info" name="contact_info"
                               class="form-control" placeholder="请输入联系方式" maxlength="100">
                    </div>
                    
                    <div class="form-group">
                        <label for="department_id">所属系部</label>
                        <select id="department_id" name="department_id" required class="form-control">
                            <option value="">请选择系部</option>
                            <?php foreach ($departments as $department): ?>
                            <option value="<?php echo $department['department_id']; ?>">
                                <?php echo htmlspecialchars($department['department_name']); ?>
                            </option>
                            <?php endforeach; ?>
                        </select>
                    </div>
                    
                    <div class="form-group">
                        <label for="age">年龄</label>
                        <input type="number" id="age" name="age" required
                               class="form-control" placeholder="请输入年龄" min="1">
                    </div>
                    
                    <div class="form-group">
                        <label for="basic_salary">基本工资</label>
                        <input type="number" id="basic_salary" name="basic_salary" required
                               class="form-control" placeholder="请输入基本工资" min="0" step="0.01">
                    </div>
                    
                    <div class="form-group">
                        <label for="bonus">奖金</label>
                        <input type="number" id="bonus" name="bonus" required
                               class="form-control" placeholder="请输入奖金" min="0" step="0.01">
                    </div>
                    
                    <div class="form-actions">
                        <button type="submit" class="btn btn-primary">保存</button>
                        <a href="teachers21.php" class="btn btn-secondary">取消</a>
                    </div>
                </form>
            </div>
        </div>
    </div>
</body>
</html>